﻿Imports System
Imports System.Security.Cryptography


Public Class EncryptTransformer
    Private algorithmID As EncryptionAlgorithm

    Private initVec As Byte()

    Private encKey As Byte()


    Friend Property IV() As Byte()
        Get
            Return initVec
        End Get

        Set(ByVal value As Byte())
            initVec = value
        End Set
    End Property

    Friend ReadOnly Property Key() As Byte()
        Get
            Return encKey
        End Get
    End Property

    Friend Sub New(ByVal algId As EncryptionAlgorithm)
        algorithmID = algId
    End Sub

    Friend Function GetCryptoServiceProvider(ByVal bytesKey As Byte()) As ICryptoTransform
        Dim iCryptoTransform As ICryptoTransform

        Select Case algorithmID
            Case EncryptionAlgorithm.Des
                Dim dES As DES = New DESCryptoServiceProvider()
                dES.Mode = CipherMode.CBC
                If bytesKey Is Nothing Then
                    encKey = dES.Key
                Else
                    dES.Key = bytesKey
                    encKey = dES.Key
                End If
                If initVec Is Nothing Then
                    initVec = dES.IV
                Else
                    dES.IV = initVec
                End If
                iCryptoTransform = dES.CreateEncryptor()
                Exit Select

            Case EncryptionAlgorithm.TripleDes
                Dim tripleDES As TripleDES = New TripleDESCryptoServiceProvider()
                tripleDES.Mode = CipherMode.CBC
                If bytesKey Is Nothing Then
                    encKey = tripleDES.Key
                Else
                    tripleDES.Key = bytesKey
                    encKey = tripleDES.Key
                End If
                If initVec Is Nothing Then
                    initVec = tripleDES.IV
                Else
                    tripleDES.IV = initVec
                End If
                iCryptoTransform = tripleDES.CreateEncryptor()
                Exit Select

            Case EncryptionAlgorithm.Rc2
                Dim rC2 As RC2 = New RC2CryptoServiceProvider()
                rC2.Mode = CipherMode.CBC
                If bytesKey Is Nothing Then
                    encKey = rC2.Key
                Else
                    rC2.Key = bytesKey
                    encKey = rC2.Key
                End If
                If initVec Is Nothing Then
                    initVec = rC2.IV
                Else
                    rC2.IV = initVec
                End If
                iCryptoTransform = rC2.CreateEncryptor()
                Exit Select

            Case EncryptionAlgorithm.Rijndael
                Dim rijndael As Rijndael = New RijndaelManaged()
                rijndael.Mode = CipherMode.CBC
                If bytesKey Is Nothing Then
                    encKey = rijndael.Key
                Else
                    rijndael.Key = bytesKey
                    encKey = rijndael.Key
                End If
                If initVec Is Nothing Then
                    initVec = rijndael.IV
                Else
                    rijndael.IV = initVec
                End If
                iCryptoTransform = rijndael.CreateEncryptor()
                Exit Select
            Case Else

                Throw New CryptographicException([String].Concat("Algorithm ID '", algorithmID, "' not supported."))
        End Select
        Return iCryptoTransform
    End Function

End Class
